<?php


namespace App\Http\Controllers;


use App\Http\Model\Hint;
use App\Http\Model\OutJson;
use App\Http\Services\MsgServices;
use App\Http\Services\MusicServices;
use App\Http\Services\RoomMsgServices;
use App\Lib\Response;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class MusicController extends BaseController
{
    protected $requestInstance;// 请求类实例
    protected $musicServices;
    protected $roomMsgService;
    protected $msgService;

    public function __construct()
    {
        $this->requestInstance = new Request();
        $this->musicServices = new MusicServices();
        $this->roomMsgService = new RoomMsgServices();
        $this->msgService = new MsgServices();
    }

    /**
     * 获取音乐
     */
    public function getMusics() {
        if (!$this->checkRequest()) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $page = $this->requestData['page'] ?? 0;
        $pageSize =  $this->requestData['page_size'] ?? 20;
        $musicName = $this->requestData['music_name'] ?? '';
        $authorName = $this->requestData['author'] ??'' ;
        $name = !empty($musicName) ? $musicName : $authorName;

        $list = $this->musicServices->getMusics($page, $pageSize, $name);
        Response::returnResult(Hint::SUCCESS, $list);
    }

    /**
     * 上传音乐
     */
    public function uploadMusic() {
        if (!$this->checkRequest()) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $musicUrl =  $this->requestData['music_url'];
        $musicName = $this->requestData['music_name'];
        $author = $this->requestData['author'] ?? '';
        $userId = $this->requestData['user_id'];
        if(!$musicUrl || !$musicName || !$userId) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $res = $this->musicServices->uploadMusic([
            'music_name' => $musicName,
            'music_url' => $musicUrl,
            'user_id' => $userId ,
            'author' =>$author
        ]);

        if($res === Hint::ERR_DATA_EXIST) Response::returnResult(Hint::ERR_DATA_EXIST);
        if($res === Hint::ERR_FAIL) Response::returnResult(Hint::ERR_FAIL);
        Response::returnResult(Hint::SUCCESS ,$res);
    }

    /**
     * 删除音乐
     *
     */
    public function deleteMusic() {
        $this->checkRequest(false);
        $musicId = (int) $this->requestData['music_id'];
        if(!$musicId) Response::returnResult(Hint::PARAM_ERROR);

        $ret = $this->musicServices->deleteMusic($musicId);
        if($ret === Hint::ERR_FAIL) Response::returnResult(Hint::ERR_FAIL);
        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 审核音乐
     */
    public function audit() {
        $this->checkRequest(false);
        $musicId = $this->requestData['music_id'];
        $status =  $this->requestData['status'];
        $userId = $this->requestData['user_id'];
        $remark = $this->requestData['remark'] ?? '';
        $checkId = $this->requestData['check_id'];

        if(!$musicId) Response::returnResult(Hint::PARAM_ERROR);
        if(!$status) Response::returnResult(Hint::PARAM_ERROR);
        if(!$userId) Response::returnResult(Hint::PARAM_ERROR);
        if(!$checkId) Response::returnResult(Hint::PARAM_ERROR);

        $ret = $this->musicServices->audit($musicId, $status,$checkId ,$remark);
        if($ret ==0) {
            if($status == 2){
                $this->msgService->send_sys_text_msg($userId ,$remark);
            }
            Response::returnResult(Hint::SUCCESS);
        }
        Response::returnResult(Hint::ERR_FAIL);
    }

    /**
     * 设置/取消用户的音乐权限
     */
    public function setPermission(){
        do {
            if (!$this->checkRequest()) {
                break;
            }

            $res = $this->callCommon($this->musicServices, 'setPermission', $this->requestData ,false);
            if($res['code'] == 0){
                $this->roomMsgService->send_music_permission($this->requestData['room_id'] ,$this->requestData['set_user_id'] ,$this->requestData['type']);
            }
        } while (false);

        if(is_string($this->responseData)) $this->responseData = json_decode($this->responseData, true);
        Response::returnResult($this->responseData['code'], $this->responseData['data'], $this->responseData['msg']);
    }

    /**
     * 播放音乐
     */
    public function playMusic(){
        if (!$this->checkRequest()) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $user_id = $this->requestData['user_id'];

        $room_id = $this->requestData['room_id'];
        if (empty($user_id) || empty($room_id)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        if (!$this->musicServices->getPermission($room_id, $user_id)) {
            Response::returnResult(Hint::AUTH_ERROR);
        }
        $this->roomMsgService->send_music_play($room_id, $user_id);

        Response::returnResult(Hint::SUCCESS);
    }
}
